/**
 *  ��Ȩ���� �人ӯ������Ƽ����޹�˾
 * ����������
 * @author ������
 */
package whu.o2o.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DevolopTool {

	public static boolean FLAG_SAVE = false;

	/**
	 * 
	 * @return ���ص�ǰ���ں�ʱ��
	 */
	public static String get_now_time() {
		String time_format = "yyyy-MM-dd-HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(time_format);
		Date date = new Date();
		String ss = sdf.format(date);
		return ss;
	}

	/**
	 * �ж�����״̬�Ƿ����
	 * 
	 * @return true:�������; false:���粻����
	 */
	public static boolean isConnectInternet(Context context) {
		ConnectivityManager conManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
		if (networkInfo != null) {
			return networkInfo.isAvailable();
		}
		return false;
	}

	/**
	 * �ж�WIFI״̬�Ƿ����
	 * 
	 * @return true:WIFI����; false:WIFI������
	 */
	public static boolean isWiFiActive(Context inContext) {
		Context context = inContext.getApplicationContext();
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getTypeName().equals("WIFI")
							&& info[i].isConnected()) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * �����жϷ����Ƿ�����.
	 * 
	 * @param context
	 * @param className
	 *            �жϵķ�������
	 * @return true ������ false ��������
	 */
	public static boolean isServiceRunning(Context mContext, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
				.getRunningServices(30);
		if (!(serviceList.size() > 0)) {
			return false;
		}
		for (int i = 0; i < serviceList.size(); i++) {
			Log.i("----�������еķ���----", serviceList.get(i).service.getClassName());
			if (serviceList.get(i).service.getClassName().equals(className) == true) {
				isRunning = true;
				break;
			}
		}
		return isRunning;
	}

	/**
	 * ����Բ�ǵ�Drawable
	 * 
	 * @param cc
	 *            ��ǰ�����Ļ���
	 * @param viewId
	 *            ͼƬID
	 * @param r
	 *            Բ�ǰ뾶
	 * @return Բ�ǵ�Drawable
	 */
	public static Drawable createCornerDrawable(Context cc, int viewId, float r) {
		Bitmap bitmap = drawableToBitmap(cc.getResources().getDrawable(viewId),
				cc);
		Bitmap bb = getRoundedCornerBitmap(bitmap, r, cc);
		return new BitmapDrawable(bb);
	}

	/**
	 * ��Drawableת����Bitmap
	 * 
	 * @param drawable
	 * @param context
	 * @return Bitmap����
	 */
	public static Bitmap drawableToBitmap(Drawable drawable, Context context) {
		Bitmap bitmap = null;
		if (null != drawable) {
			bitmap = Bitmap
					.createBitmap(
							drawable.getIntrinsicWidth(),
							drawable.getIntrinsicHeight(),
							drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
									: Bitmap.Config.RGB_565);
			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
					drawable.getIntrinsicHeight());
			drawable.draw(canvas);
		} else {
			bitmap = BitmapFactory.decodeResource(context.getResources(), 0);
		}
		return bitmap;
	}

	/**
	 * ���Բ��ͼƬ�ķ���
	 * 
	 * @param bitmap
	 *            Բ�ǵ�Bitmap
	 * @param roundPx
	 *            Բ�ǰ뾶
	 * @param context
	 * @return Բ�ǵ�Bitmap����
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx,
			Context context) {
		Bitmap output = null;
		if (null != bitmap) {
			output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
					Config.ARGB_8888);
			Canvas canvas = new Canvas(output);

			final int color = 0xff424242;
			final Paint paint = new Paint();
			final Rect rect = new Rect(0, 0, bitmap.getWidth(),
					bitmap.getHeight());
			final RectF rectF = new RectF(rect);

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rect, rect, paint);
		} else {
			output = BitmapFactory.decodeResource(context.getResources(), 0);
		}
		return output;
	}

	/**
	 * �����ֻ��ķֱ��ʴ� dp �ĵ�λ ת��Ϊ px(����)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * Copyright (C) 2013 by Azd
	 * 
	 * ��ʾ��Ϣ����
	 */
	public static void ShowType(boolean[] layout_type_state,
			LinearLayout[] layout_type) {
		for (int i = 0; i < layout_type_state.length; i++) {
			if (!layout_type_state[i]) {
				layout_type[i].setVisibility(View.GONE);
			} else {
				layout_type[i].setVisibility(View.VISIBLE);
			}
		}
	}

	/**
	 * Copyright (C) 2013 by Azd
	 * 
	 * ��֤�û������������Ϣ����
	 */
	public static boolean checkInput(Context context, String type,
			String e_mail_values, EditText e_mail) {
		boolean result = true;

		if (type.equals("�����ַ")) {
			result = isEmail(e_mail_values);
			if (!result) {
				Toast.makeText(context, "��" + type + "����ʽ����ȷ�����������룡",
						Toast.LENGTH_SHORT).show();
				e_mail.requestFocus();
			}
		}
		return result;
	}

	/**
	 * Copyright (C) 2013 by Azd
	 * 
	 * �ж�e_mail��ʽ�Ƿ���ȷ
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}

	/**
	 * Copyright (C) 2013 by Azd
	 * 
	 * ��ֹ�ؼ����ظ����
	 */
	private static long lastClickTime;
	private static int lastResource;

	public static boolean isFastDoubleClick(int id) {
		long time = System.currentTimeMillis();
		long timeD = time - lastClickTime;
		if (0 < timeD && timeD < 1000 && lastResource == id) {
			return true;
		}
		lastClickTime = time;
		lastResource = id;
		return false;
	}

	/**
	 * Copyright (C) 2013 by Azd
	 * 
	 * ���ؽ�����
	 */
	private static ProgressDialog pg;

	public static void showProgress(Context context, String msg, boolean flag) {
		if (pg == null) {
			pg = new ProgressDialog(context);
		}
		if (flag) {
			pg.setMessage(msg);
			pg.setCanceledOnTouchOutside(false);
			pg.show();
		} else {
			if (pg.isShowing()) {
				pg.dismiss();
			}
			pg = null;
		}
	}


}
